package com.Register.controller.user;

import com.Register.constant.JwtClaimsConstant;
import com.Register.dto.UserAppletLoginDTO;
import com.Register.dto.UserRegisterDTO;
import com.Register.dto.WxUserDTO;
import com.Register.entity.WxUser;
import com.Register.exception.LoginFailedException;
import com.Register.properties.JwtProperties;
import com.Register.result.Result;
import com.Register.service.UserService;
import com.Register.utils.JwtUtil;
import com.Register.vo.CheckBindVO;
import com.Register.vo.UserAppletLoginVO;
import com.Register.vo.WxUserVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name="用户接口")
@RestController("appletUserController")
@RequestMapping("/applet/user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private JwtProperties jwtProperties;

    /**
     * 用户注册相关接口
     * @param userRegisterDTO
     * @return
     */
    @Operation(summary = "用户注册账号")
    @PostMapping("/register")
    public Result<String> Register(@RequestBody UserRegisterDTO userRegisterDTO){
        log.info("用户注册");
        if(userService.checkUserExists(userRegisterDTO.getUsername())!=null){
            return Result.error("用户名已经存在");
        }
        else{
            return userService.userRegister(userRegisterDTO);
        }
    }

    /**
     *  微信登录
     * @param userAppletLoginDTO 微信登录前端传的DTO
     * @return userLoginVO
     */
    @Operation(summary = "微信登录")
    @PostMapping("/wxLogin")
    public Result<UserAppletLoginVO> login(@RequestBody UserAppletLoginDTO userAppletLoginDTO){
        log.info("微信用户登录:{}",userAppletLoginDTO.getCode());

        //微信登录
        //登录时同步用户信息
        try {
            WxUser user = userService.wxLogin(userAppletLoginDTO);
            //为微信用户生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.WX_USER_ID, user.getId());
            String token = JwtUtil.createJWT(jwtProperties.getAppletSecretKey(), jwtProperties.getAppletTtl(), claims);
            System.out.println("token:" + token);
            UserAppletLoginVO userLoginVO = UserAppletLoginVO.builder()
                    .id(user.getId())
                    .openid(user.getOpenid())
                    .token(token)
                    .build();

            return Result.success(userLoginVO);
        }catch (LoginFailedException e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 检查用户是否绑定，已绑定返回1，未绑定返回0
     * @return 返回绑定信息，已绑定返回1，未绑定返回0
     */
    @GetMapping("/checkBind")
    @Operation(summary = "检查用户是否绑定")
    public Result<CheckBindVO> check(){
        CheckBindVO checkBindVO = userService.checkBind();
        return Result.success(checkBindVO);
    }

    /**
     * 获取用户个人信息
     * @return 返回用户信息
     */
    @GetMapping("/info")
    @Operation(summary = "获取用户个人信息")
    public Result<WxUserVO> getUserInfo() throws JsonProcessingException {
        return Result.success(userService.getUserInfo());
    }

    /*
    更新微信用户信息
     */
    @Operation(summary = "更新个人信息")
    @PutMapping("/update")
    public Result<String> wxUserUpdate(@RequestBody WxUserDTO wxUserDTO){
        return userService.wxUserUpdate(wxUserDTO);
    }

    /**
     * 用户退出
     */
    @PostMapping("/logout")
    @Operation(summary = "用户退出")
    public Result<String> logout() {
        log.info("用户退出");
        return Result.success();
    }
}
